package com.burgess.net.proxy;

import com.burgess.net.model.Boy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * JDK动态代理代码实现
 * 创建人    BurgessLee
 * 创建时间   2020/5/24
 * 描述
 */
public class PeopleProxy implements InvocationHandler {

    private People people;

    public PeopleProxy(People people){
        this.people = people;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("I am proxy People");
        Object invoke = method.invoke(people, args);
        System.out.println("I am proxy complete");
        return invoke;
    }

    public static void main(String[] args) {
        People people = new Boy();
        PeopleProxy peopleProxy = new PeopleProxy(people);
        People proxyInstance = (People)Proxy.newProxyInstance(people.getClass().getClassLoader(), people.getClass().getInterfaces(),
                peopleProxy);
        System.out.println(proxyInstance);
    }
}
